package com.hcc.repository.gen.generator;

import com.hcc.repository.gen.config.GlobalConfig;
import com.hcc.repository.gen.config.PackageConfig;
import com.hcc.repository.gen.config.TemplateConfig;
import com.hcc.repository.gen.engine.TemplateEngine;
import com.hcc.repository.gen.model.ColumnMetadata;
import com.hcc.repository.gen.model.TableMetadata;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Map;

/**
 * AbstractGenerator
 *
 * @author hushengjun
 * @date 2023/9/28
 */
@Slf4j
public abstract class AbstractGenerator implements Generator {

    protected String templatePath;

    @Override
    public void setTemplatePath(String templatePath) {
        this.templatePath = templatePath;
    }

    @Override
    public String getTemplatePath() {
        return templatePath;
    }

    @Override
    public void generate(TableMetadata tableMetadata, GlobalConfig globalConfig) {
        if (!needGenerate(globalConfig)) {
            log.debug(generateType() + " ---> 不生成");
            return;
        }

        PackageConfig packageConfig = globalConfig.getPackageConfig();

        String filePath = packageConfig.getSrcPath()
                + File.separator
                + this.getPackageFileName(tableMetadata, globalConfig);

        File srcFile = new File(filePath);
        if (srcFile.exists() && !this.needOverride(globalConfig)) {
            log.debug(generateType() + " ---> 不覆盖");
            return;
        }

        TemplateEngine templateEngine = globalConfig.getTemplateConfig().getTemplateEngine();
        templateEngine.generateFile(this.getDataModel(tableMetadata, globalConfig), templatePath, srcFile);

        log.debug(generateType() + " ---> " + srcFile);
    }

    protected abstract boolean needGenerate(GlobalConfig globalConfig);
    protected abstract boolean needOverride(GlobalConfig globalConfig);
    protected abstract String getPackageFileName(TableMetadata tableMetadata, GlobalConfig globalConfig);
    protected abstract Map<String, Object> getDataModel(TableMetadata tableMetadata, GlobalConfig globalConfig);

    public String generateType() {
        return "";
    }

}
